package org.hu.controller;

import lombok.extern.slf4j.Slf4j;
import org.hu.entity.dto.OrderAmountQueryDTO;
import org.hu.entity.dto.OrderDetailAddDTO;
import org.hu.entity.dto.OrderDetailPageQueryDTO;
import org.hu.entity.dto.OrderPageQueryDTO;
import org.hu.entity.pojo.Order;
import org.hu.entity.pojo.OrderDetail;
import org.hu.entity.pojo.PageBean;
import org.hu.entity.vo.DailyStats;
import org.hu.entity.vo.MonthlyStats;
import org.hu.entity.vo.Result;
import org.hu.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * &#064;author:  HYH
 * @date:  2020/7/27 17:04
 */
@RestController()
@RequestMapping("/order")
@CrossOrigin
@Slf4j
public class OrderController {

    @Autowired
    private OrderService orderService;

    // 新增订单
    @PostMapping("/add")
    public Result<String> add(@RequestBody OrderDetailAddDTO orderDetailAddDTO) {
        log.info("新增订单:{}", orderDetailAddDTO);
        orderService.add(orderDetailAddDTO);
        return Result.success("下单成功");
    }

    // 删除一条订单
    @DeleteMapping("/delete/{id}")
    public Result<String> delete(@PathVariable Integer id) {
        log.info("删除订单:{}",id);
        orderService.delete(id);
        return Result.success("删除成功");
    }

    // 修改订单
    @PostMapping("/update")
    public Result<String> update(@RequestBody OrderDetail orderDetail) {
        log.info("修改订单详情:{}", orderDetail);
        orderService.update(orderDetail);
        return Result.success("修改成功");
    }

    // 条件分页查询订单细则
    @PostMapping("/list")
    public  Result<PageBean> list(@RequestBody OrderDetailPageQueryDTO orderDetailPageQueryDTO) {
        log.info("分页查询订单细则:{}", orderDetailPageQueryDTO);
        PageBean pageBean = orderService.list(orderDetailPageQueryDTO);
        return Result.success("查询成功",pageBean);
    }

    // 条件分页查询订单
    @PostMapping("/listOrder")
    public  Result<PageBean> listOrder(@RequestBody OrderPageQueryDTO orderPageQueryDTO) {
        log.info("分页查询订单:{}", orderPageQueryDTO);
        PageBean pageBean = orderService.listOrder(orderPageQueryDTO);
        return Result.success("查询成功",pageBean);
    }

    // 修改订单
    @PostMapping("/updateOrder")
    public Result<String> updateOrder(@RequestBody Order order) {
        log.info("修改订单:{}", order);
        orderService.updateOrderById(order);
        return Result.success("修改成功");
    }

    // 当订单完成,根据订单id来修改菜品的月售量
    @PostMapping("/updateDishMonthSale")
    public Result<String> updateDishMonthSale(Integer id) {
        log.info("要修改菜品月售的订单id:{}", id);
        orderService.updateDishMonthSale(id);
        return Result.success("修改成功");
    }

    // 获取某年每个月的销售数据
    @GetMapping("/monthly")
    public Result<List<MonthlyStats>> getMonthly(@RequestParam int year) {
        log.info("要查询的年份:{}", year);
        List<MonthlyStats> list = orderService.getMonthlyStats(year);
        return Result.success("查询成功", list);
    }
    // 获取某月每天的销售数据
    @GetMapping("/daily")
    public Result<List<DailyStats>> getDaily(@RequestParam int year, @RequestParam int month) {
        log.info("要查询的年月:{}-{}",year, month);
        List<DailyStats> list = orderService.getDailyStats(year, month);
        return Result.success("查询成功", list);
    }
}
